import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:provider/provider.dart';
import 'package:qqjdflutter/config/jd_api.dart';
import 'package:qqjdflutter/model/home_page_model.dart';
import 'package:qqjdflutter/net/net_request.dart';
import 'package:qqjdflutter/provider/home_page_provider.dart';
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
   // NetRequest();
   // NetRequest().reqeustData(JdApi.HOME_PAGE);
    //NetRequest().reqeustData(JdApi.HOME_PAGE).then((res)=>print(res.data));
    return ChangeNotifierProvider<HomePageProvider>(create: (context){
      var provider = new HomePageProvider();
      provider.loadHomePageData();
      return provider;
    },
     child: Scaffold(
       appBar: AppBar(
         title: Text("首页"),
       ),
       body:Container(
         color: Color(0xFFf4f4f4),
         child: Consumer<HomePageProvider>(builder: (_,provider,__){
           // 获取接口返回
           //print(provider.isLoading);
          // print(provider.isError);
           // 加载动画
           if(provider.isLoading){
             return Center(child: CupertinoActivityIndicator());
           }

           // 捕获异常
           if(provider.isError){
             return Center(
               child: Column(
                 mainAxisAlignment: MainAxisAlignment.center,
                 children: [
                   Text(provider.errorMsg),
                   OutlinedButton(
                     onPressed: (){
                     provider.loadHomePageData();
                   }, child: Text("刷新"))
                 ],
               ),
             );
           }
           HomePageModel? model = provider.model; //!!
           print(model?.toJson()); // !!
           return ListView(
             children: [
               // 录播图
               buildAspectRatio(model!),
               // 图标分类
               buildLogos(model),
               // 掌上秒杀头部
               bulidMSHeaderContainer(),
               // 掌上秒杀商品
               buildMSBodyContainer(model),
               // 广告1
               buildAds(model.pageRow.ad1),
               // 广告2
               buildAds(model.pageRow.ad2),

             ],
           );

           return Container();
         },),
       )
     ),
    );
  }

  // 广告

  Widget buildAds(List<String> ads){
    List<Widget> list = [];
    for(var i = 0;i< ads.length;i++){
      list.add(Expanded(
        child: Image.asset("assets${ads[i]}"),
      ));
    }
    return Row(
      children: list,
    );
  }
  // 掌上秒杀商品
  Container buildMSBodyContainer(HomePageModel model){
    return Container(
      height: 120,
      color: Colors.white,
      child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: model.quicks.length,
          itemBuilder: (context,index){
            return Padding(
                padding: EdgeInsets.all(5.0),
              child: Column(
                children: [
                  Image.asset(
                    "assets${model.quicks[index].image}",
                    width: 85,
                    height: 85,
                  ),
                  Text(
                    "${model.quicks[index].price}",
                    style: TextStyle(color: Colors.red,fontSize: 16.0),
                  )
                ],
              ),
            );

      }),
    );
  }

  // 掌上秒杀头部
  Container bulidMSHeaderContainer(){
    return Container(
      margin: EdgeInsets.only(top: 10.0),
      padding: EdgeInsets.all(10.0),
      color: Colors.white,
      height: 50,
      child: Row(
        children: [
          Image.asset(
            "assets/image/bej.png",
            width: 90,
            height: 20,
          ),
          Spacer(), // ??
          Text("更多秒杀"),
          Icon(CupertinoIcons.right_chevron,size: 14)
        ],
      ),
    );
  }

  // 图标分类
  Widget buildLogos(HomePageModel model){
    // 定义list数组 列表容器
    List<Widget> list = [];
    // 遍历 model
    for(var i =0; i<model.logos.length;i++){
      list.add(Container(
        width: 60.0,
        child: Column(
          children: [
            Image.asset(
              "assets${model.logos[i].image}",
              width: 50,
              height: 50,
            ),
            Text("${model.logos[i].title}")
          ],
        ),
      ));
    }
    return Container(
      color: Colors.white,
      height: 170,
      padding: EdgeInsets.all(10.0),
      child: Wrap(
        spacing: 7.0,
        runSpacing: 10.0,
        alignment: WrapAlignment.spaceBetween,
        children: list,
      ),
    );
  }

  // 轮播图
  AspectRatio buildAspectRatio(HomePageModel model){
    return AspectRatio(
      aspectRatio: 72/35,
      child: Swiper(
        itemCount: model.swipers.length,
        pagination: SwiperPagination(),
        autoplay: true,
        itemBuilder: (BuildContext context,int index){
          return Image.asset("assets${model.swipers[index].image}");
        },
      ),
    );
  }
}

// NetRequest() async{
//   var dio = Dio();
//   Response response = await dio.get(JdApi.HOME_PAGE);
//   print(response.data);
// }

